package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.log.LoggerUtils;

/**
 * @author dimitri
 */
public class PrepareTaskForPanel extends javax.swing.JPanel {
    private User mainUser;
    private User ownerUser;

    public PrepareTaskForPanel() {
        initComponents();
        enableComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        btnMyself = new javax.swing.JRadioButton();
        btnMyBoss = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        userTableBase1 = new ge.telasi.tasks.ui.user.UserTableBase();

        jLabel1.setText("დავალება განკუთვნილია:");

        buttonGroup1.add(btnMyself);
        btnMyself.setSelected(true);
        btnMyself.setText("ჩემთვის");
        btnMyself.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMyselfActionPerformed(evt);
            }
        });

        buttonGroup1.add(btnMyBoss);
        btnMyBoss.setText("ჩემი უფროსისთვის:");
        btnMyBoss.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMyBossActionPerformed(evt);
            }
        });

        userTableBase1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(userTableBase1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(btnMyself)
                    .addComponent(btnMyBoss))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnMyself)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnMyBoss)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnMyselfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMyselfActionPerformed
        enableComponents();
    }//GEN-LAST:event_btnMyselfActionPerformed

    private void btnMyBossActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMyBossActionPerformed
        enableComponents();
    }//GEN-LAST:event_btnMyBossActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton btnMyBoss;
    private javax.swing.JRadioButton btnMyself;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private ge.telasi.tasks.ui.user.UserTableBase userTableBase1;
    // End of variables declaration//GEN-END:variables

    private void enableComponents() {
        btnMyBoss.setEnabled(userTableBase1.getRowCount() > 0);
        jScrollPane1.setVisible(btnMyBoss.isSelected());
        doLayout();
    }

    public void setMainUser(User mainUser) {
        this.mainUser = mainUser;
        refresh();
    }

    public void setOwnerUser(User ownerUser) {
        this.ownerUser = ownerUser;
        if (ownerUser != null) {
            if (ownerUser.equals(mainUser)) {
                btnMyself.setSelected(true);
            } else {
                btnMyBoss.setSelected(true);
                userTableBase1.setSelectedUser(ownerUser);
            }
        }
        enableComponents();
    }

    public User getUser() {
        if (btnMyBoss.isSelected() && userTableBase1.getSelectedUser() != null) {
            return userTableBase1.getSelectedUser();
        }
        return mainUser;
    }

    private void refresh() {
        Platform pl = Platform.getDefault();
        if (pl.validateConnection() && mainUser != null) {
            try {
                mainUser = pl.getFacade().getUserWithRelations(pl.getCredentials(), mainUser);
                userTableBase1.display(mainUser.getBosses());
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
        enableComponents();
    }

}
